package com.xiao.controller;

import com.xiao.pojo.Appointment;
import com.xiao.pojo.Books;
import com.xiao.pojo.Borrow;
import com.xiao.pojo.Student;
import com.xiao.service.AppointService;
import com.xiao.service.AppointServiceImpl;
import com.xiao.service.BookService;
import com.xiao.service.BookServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointController {
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService=new BookServiceImpl();



    @RequestMapping("/addAppoint")
    public ModelAndView addAppoint(int id, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(id);
    Books books = bookService.queryBookById(id);
    System.out.println(2222);
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Student stu = (Student) session.getAttribute("stu");
  String bname = books.getBookName();
        int bid = books.getBookID();
        String sno = stu.getSno();
        String sname = stu.getSname();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String adate = df.format(new Date());
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        long nowDate = new Date().getTime();
        String aid = Integer.toHexString((int) nowDate);//随机生成预约id
    Appointment app = new Appointment(aid,bid,bname,sno,sname,adate,0);
    bookService.addAppoint(app);
  bookService.reduceBook(id);
        return new ModelAndView("redirect:/appointment/Appoint-list");
    }
    @RequestMapping("/Appoint-list")
    public ModelAndView appointList(HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Student stu = (Student) session.getAttribute("stu");
        String sno = stu.getSno();
        List<Appointment> appointmentList = bookService.queryAllAppointBySno(sno);
        mv.addObject("appointmentList", appointmentList);
        mv.setViewName("a-list");

        return mv;
    }
@RequestMapping("/cancelAppoint")
    public String ReturnBook(String aid,int bid){
        bookService.increaseBook(bid);
        bookService.deleteAppoint(aid);
        return "redirect:/appointment/Appoint-list";
    }

    @RequestMapping("/Appoint-alllist")
    public ModelAndView appointallList(HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();


        List<Appointment> appointmentList = bookService.queryAllAppoint();
        mv.addObject("appointmentList", appointmentList);
        mv.setViewName("appoint-allList");

        return mv;
    }
@RequestMapping("/changeState")
public String changeState(@Param("aid") String aid,@Param("state") int state) {
    System.out.println(666);
bookService.changeState(aid,state);
    return "redirect:/appointment/Appoint-alllist";
    }

    @RequestMapping("/borrow-admin")
    public String borrowadmin(@Param("bookID") int bookID,@Param("bookName") String bookName,@Param("sno") String sno,@Param("sname") String sname,@Param("aid") String aid) {
        Borrow borrow = new Borrow(bookID, bookName, sno, sname);
        bookService.addBorrow(borrow);
        bookService.deleteAppoint(aid);
        return "redirect:/appointment/Appoint-alllist";
    }
}
